const router = require('koa-router')()
const { getList } = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel') 

const loginCheck = require('../middleware/loginCheck')
router.prefix('/api/blog')
 
// 查询博客
router.get('/list', async function (ctx, next) {
    let author = ctx.query.author || ''
    const keyword = ctx.query.keyword || ''
    const listData = await getList(author, keyword)
    ctx.body = new SuccessModel(listData, '成功')
})


// 查询自己的博客
router.get('/mylist',loginCheck, async function (ctx, next) {
    if (ctx.state.user) {
        const author = ctx.state.user.name
        const listData = await getList(author)
        ctx.body = new SuccessModel(listData)
    } else {
        ctx.body = new ErrorModel('找不到用户，查询失败')
    }
})


module.exports = router